package com.yunzheng.controller;

import com.yunzheng.common.BaseResult;
import com.yunzheng.dto.ChangePlanDTO;
import com.yunzheng.dto.ChangePlanOrderDTO;
import com.yunzheng.dto.NewPlanDTO;
import com.yunzheng.entity.ProjectPlan;
import com.yunzheng.service.ProjectPlanService;
import com.yunzheng.util.SnowFlakeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 郭思良
 * @create 2020-07-30 17:41
 */

@RestController
@RequestMapping("/plan")
@Api(tags = "项目计划管理")
@Slf4j
public class ProjectPlanController {

	@Resource
	private ProjectPlanService projectPlanService;

	/**
	 * 向后台传输 所属项目id、计划阶段名、开始时间、结束时间
	 * <p>
	 * /post -- roles[2]
	 */
	@ApiOperation("1.新建项目计划阶段")
	@PostMapping("/post")
	public BaseResult postNewPlan(@Valid @RequestBody NewPlanDTO input) {
		ProjectPlan plan = new ProjectPlan();
		plan.setPlanId(SnowFlakeUtil.nextId()).setProjectId(input.getProjectId()).setPlanName(input.getPlanName()).setPlanStart(input.getPlanStart())
				.setPlanEnd(input.getPlanEnd()).setSort(input.getSort());
		projectPlanService.save(plan);
		return BaseResult.ok();
	}

	/**
	 * 删除项目计划阶段
	 * <p>
	 * /remove -- roles[2]
	 */
	@ApiOperation("2.删除项目计划阶段")
	@DeleteMapping("/remove")
	public BaseResult removePlan(String planId) {
		projectPlanService.removeById(planId);
		return BaseResult.ok();

	}

	/**
	 * 向后台传输项目计划id、计划阶段名、开始时间、结束时间
	 * <p>
	 * /change -- roles[2]
	 */
	@ApiOperation("3.修改项目计划阶段基本信息")
	@PutMapping("/change")
	public BaseResult changePlan(@Valid @RequestBody ChangePlanDTO input) {
		ProjectPlan projectPlan = new ProjectPlan();
		BeanUtils.copyProperties(input, projectPlan);
		projectPlanService.saveOrUpdate(projectPlan);
		return BaseResult.ok();
	}

	/**
	 * 修改项目计划顺序
	 * <p>
	 * /change/order -- roles[2]
	 */
	@ApiOperation("4.修改项目计划顺序")
	@PutMapping("/change/order")
	public BaseResult changePlanOrder(@Valid @RequestBody List<ChangePlanOrderDTO> inputs) {
		List<ProjectPlan> plans = new ArrayList<>();
		for (ChangePlanOrderDTO input : inputs) {
			ProjectPlan plan = new ProjectPlan();
			BeanUtils.copyProperties(input, plan);
			plans.add(plan);
		}
		projectPlanService.updateBatchById(plans);
		return BaseResult.ok();
	}


	/**
	 * 显示某个项目的具体项目计划，自定义所有项目阶段。
	 * <p>
	 * /show -- authc
	 */
	@ApiOperation("5.展示某个项目的项目计划")
	@GetMapping("/show")
	public BaseResult getProjectPlan(String projectId) {
		return BaseResult.ok(projectPlanService.showProjectPlan(projectId));
	}

}
